//
//  PGShopViewController.m
//  PhotographicGuide
//
//  Created by indream on 13-2-26.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGShopViewController.h"
#import "MobClick.h"
@interface PGShopViewController ()

@end

@implementation PGShopViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id)init{        
    self = [super initWithNibName:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?@"PGShopViewController":@"PGShopViewController_iPad" bundle:nil];
    if(self){
        
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if(_url==nil){
        _url = @"http://1.starrynightwalk.sinaapp.com/?p=62";
    }
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
    if(self.title){
        _titleLabel.text = self.title;
    }
    
    _backButton.enabled =  NO;
    _forwardButton.enabled =  NO;
    UISwipeGestureRecognizer *swiper = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
    [self.view addGestureRecognizer:swiper];
    // Do any additional setup after loading the view from its nib.
}
-(void)swipeHandle:(UISwipeGestureRecognizer*)gesture{
    if(gesture.direction==UISwipeGestureRecognizerDirectionRight){
        [self backHandle:nil];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:_url];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:_url];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTitleLabel:nil];
    [super viewDidUnload];
}
#pragma mark - webview delegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [_activityView startAnimating];
    _backButton.enabled =  _webView.canGoBack;
    _forwardButton.enabled =  _webView.canGoForward;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [_activityView stopAnimating];
    _backButton.enabled =  _webView.canGoBack;
    _forwardButton.enabled =  _webView.canGoForward;
    
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if(self.title){
        _titleLabel.text = self.title;
    }else{
        _titleLabel.text = title;
    }
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    _backButton.enabled =  _webView.canGoBack;
    _forwardButton.enabled =  _webView.canGoForward;
    [_activityView stopAnimating];
}
- (IBAction)backHandle:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
